<?php

// $Id$


require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_menu_item.class.php');
require_once('tcms_menu_item_type_dbc.class.php');


class TCMS_Menu_Item_DBC extends TCMS_Menu_Item implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);

		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select menu, ".
						"parent, ".
						"label, ".
						"published, ".
						"ordering, ".
						"type, ".
						"new_window, ".
						"nofollow, ".
						"additional ".
					"from tcms_menu_items where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_menu($rez_obj->MENU);
			$this->set_parent($rez_obj->PARENT);
			$this->set_label(stripslashes($rez_obj->LABEL));
			$this->set_published($rez_obj->PUBLISHED);
			$this->set_ordering($rez_obj->ORDERING);
			$this->set_type($rez_obj->TYPE);
			$this->set_new_window($rez_obj->NEW_WINDOW);
			$this->set_nofollow($rez_obj->NOFOLLOW);
			$this->set_additional(stripslashes($rez_obj->ADDITIONAL));
			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_menu_items_seq');
		$menu = $this->get_menu();
		$parent = $this->get_parent();
		$label = addslashes($this->get_label());
		$published = $this->get_published() ? 1 : 0;
		$ordering = $this->get_ordering();
		$type = $this->get_type();
		$new_window = $this->get_new_window();
		$nofollow = $this->get_nofollow() ? 1 : 0;
		$additional = addslashes($this->get_additional());

		if ($id) {
			$sql = "insert into tcms_menu_items (id, ".
									      "menu, ".
									      "parent, ".
									      "label, ".
									      "published, ".
									      "ordering, ".
									      "type, ".
									      "new_window, ".
									      "nofollow, ".
									      "additional ".
									      ") ".
								"values ".
									      "($id,".
									      "$menu, ".
									      "$parent, ".
									      "'$label', ".
									      "$published, ".
									      "$ordering, ".
									      "$type, ".
									      "$new_window, ".
									      "$nofollow, ".
									      "'$additional' ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_menu_items_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$menu = $this->get_menu();
		$parent = $this->get_parent();
		$label = addslashes($this->get_label());
		$published = $this->get_published() ? 1 : 0;
		$ordering = $this->get_ordering();
		$type = $this->get_type();
		$new_window = $this->get_new_window();
		$nofollow = $this->get_nofollow() ? 1 : 0;
		$additional = addslashes($this->get_additional());

		$sql = "update tcms_menu_items set ".
						"menu = $menu, ".
						"parent = $parent, ".
						"label = '$label', ".
						"published = $published, ".
						"ordering = $ordering, ".
						"type = $type, ".
						"new_window = $new_window, ".
						"nofollow = $nofollow, ".
						"additional = '$additional' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_menu_items where id = $id";
		$dbc->execute($sql);
	}


	public static function load_menu_items(DB_Connection $dbc, $menu_id) {
		tangra_if_not_int_throw_e($menu_id);

		$sql = "select id from tcms_menu_items where menu = $menu_id and parent = 0";
		$rez = $dbc->execute($sql);

		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$tmp = new TCMS_Menu_Item_DBC();
			$tmp->load_by_id($dbc, $rez_obj->ID);
			$ret[] = clone $tmp;
		}

		return $ret;
	}


	public static function load_menu_items_full(DB_Connection $dbc, $menu_id, $hidden_inc_dir, $only_published = false) {
		tangra_if_not_int_throw_e($menu_id);

		if ($only_published) {
			$published = 'and published = 1 ';
		} else {
			$published = '';
		}

		$sql = "select id from tcms_menu_items where menu = $menu_id and parent = 0 $published order by ordering asc";
		$rez = $dbc->execute($sql);

		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$ret[] = TCMS_Menu_Item_DBC::load_by_id_full($dbc, $rez_obj->ID, $hidden_inc_dir, $only_published);

//			$tmp = new TCMS_Menu_Item_DBC();
//			$tmp->load_by_id_full($dbc, $rez_obj->ID);
//			$ret[$c]['item'] = clone $tmp;
//
//			$tmp2 = new TCMS_Menu_Item_Type_DBC();
//			$tmp2->load_by_id($dbc, $tmp->get_type());
//			$ret[$c]['type'] = clone $tmp2;
//
//			$ret[$c]['url'] = TCMS_Menu_Item_DBC::fetch_url($dbc, $rez_obj->ID, $tmp2, $hidden_inc_dir);
		}

		return $ret;
	}

	public static function load_by_id_full(DB_Connection $dbc, $id, $hidden_inc_dir, $only_published = false) {
		$ret = array();

		$tmp = new TCMS_Menu_Item_DBC();
		$tmp->load_by_id($dbc, $id);

		$tmp2 = new TCMS_Menu_Item_Type_DBC();
		$tmp2->load_by_id($dbc, $tmp->get_type());
		$ret['type'] = clone $tmp2;

		$spec_item = TCMS_Menu_Item_DBC::load_specific_item($dbc, $id, $tmp2, $hidden_inc_dir);
		$url_raw = $spec_item->get_url($dbc);
		if (substr($url_raw, 0, 7) == 'http://' || substr($url_raw, 0, 8) == 'https://') {
			$url_parsed = parse_url($url_raw);
			if ($url_parsed !== false) {
				$ret['url'] = $url_parsed['scheme'].'://'.$url_parsed['host'].str_replace('%2F', '/',urlencode($url_parsed['path']));
				if (array_key_exists('query', $url_parsed)) {
					$ret['url'] .= '?'.$url_parsed['query'];
				}
			} else {
				$ret['url'] = '';
			}
		} else {
			$ret['url'] = str_replace('%2F', '/', urlencode($spec_item->get_url($dbc)));
		}

		if ($only_published) {
			$published = 'and published = 1 ';
		} else {
			$published = '';
		}

		$sql = "select id from tcms_menu_items where parent = $id $published order by ordering asc";
		$rez = $dbc->execute($sql);
		while($rez_obj = $rez->fetch_object()) {
			$child = TCMS_Menu_Item_DBC::load_by_id_full($dbc, $rez_obj->ID, $hidden_inc_dir);
			$spec_item->add_subitem($child);
		}
		$ret['item'] = clone $spec_item;

		return $ret;
	}


	private static function load_specific_item(DB_Connection $dbc, $item_id, TCMS_Menu_Item_Type_DBC $type, $hidden_inc_dir) {
		$ret = '';

		$class_name = $type->get_class_name();

		if (!class_exists($class_name)) {
			require_once($hidden_inc_dir.$type->get_class_path());
		}

		$tmp = new $class_name;
		$tmp->load_by_id($dbc, $item_id);

		return $tmp;
	}


	public static function delete_cascade(DB_Connection $dbc, $id, $hidden_inc_dir) {
		tangra_if_not_int_throw_e($id);

		$item = new TCMS_Menu_Item_DBC();
		if ($item->load_by_id($dbc,$id)) {
			$type = new TCMS_Menu_Item_Type_DBC();
			if (!$type->load_by_id($dbc, $item->get_type())) {
				throw new Tangra_Module_Exception('Cannot load TCMS_Menu_Item_Type_DBC with id = '.$item->get_type());
			}

			$class_name = $type->get_class_name();
			if (!class_exists($class_name)) {
				require_once($hidden_inc_dir.$type->get_class_path());
			}

			$tmp = new $class_name;
			$tmp->delete($dbc, $id);

			$sql = "select id from tcms_menu_items where parent = $id ";
			$rez = $dbc->execute($sql);
			while ($rez_obj = $rez->fetch_object()) {
				TCMS_Menu_Item_DBC::delete_cascade($dbc, $rez_obj->ID, $hidden_inc_dir);
			}
		}
	}


	public static function get_next_order(DB_Connection $dbc, $parent_id, $menu_id) {
		tangra_if_not_int_throw_e($parent_id);
		tangra_if_not_int_throw_e($menu_id);

		$ret = 10;

		$sql = "select ordering from tcms_menu_items where menu = $menu_id and parent = $parent_id order by ordering desc";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = ((int)($rez_obj->ORDERING / 10) + 1)*10; //next ordering calculation
		}

		return $ret;
	}


	public static function move_up(DB_Connection $dbc, $id, $menu_id) {
		$current = new TCMS_Menu_Item_DBC();

		if ($current->load_by_id($dbc, $id)) {
			$sql = "select id from tcms_menu_items where "
						."parent = ".$current->get_parent()
						." and ordering < ".$current->get_ordering()
						." order by ordering desc";

			$rez = $dbc->execute($sql);
			if (!$rez->is_eod()) {
				$above = new TCMS_Menu_Item_DBC();
				$rez_obj = $rez->fetch_object();
				$above->load_by_id($dbc, $rez_obj->ID);

				// Swaping orders
				$tmp = $current->get_ordering();
				$current->set_ordering($above->get_ordering());
				$above->set_ordering($tmp);

				$current->save($dbc);
				$above->save($dbc);
			}
		}
	}


	public static function move_down(DB_Connection $dbc, $id, $menu_id) {
		$current = new TCMS_Menu_Item_DBC();

		if ($current->load_by_id($dbc, $id)) {
			$sql = "select id from tcms_menu_items where "
						."parent = ".$current->get_parent()
						." and ordering > ".$current->get_ordering()
						." order by ordering asc";
			$rez = $dbc->execute($sql);
			if (!$rez->is_eod()) {
				$above = new TCMS_Menu_Item_DBC();
				$rez_obj = $rez->fetch_object();
				$above->load_by_id($dbc, $rez_obj->ID);

				// Swaping orders
				$tmp = $current->get_ordering();
				$current->set_ordering($above->get_ordering());
				$above->set_ordering($tmp);

				$current->save($dbc);
				$above->save($dbc);
			}
		}
	}


	public static function publish(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "update tcms_menu_items set published = 1 where id = $id";
		$rez = $dbc->execute($sql);
	}



	public static function unpublish(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "update tcms_menu_items set published = 0 where id = $id";
		$rez = $dbc->execute($sql);
	}
}